package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    //新增菜品同时保存口味数据

    @Transactional
    public void saveWithFlavor(DishDto dishDto, int type) {
        //保存菜品基本信息到DISH表
        dishDto.setNewPrice(dishDto.getPrice());
        dishDto.setNumber(BigDecimal.valueOf(0));

        super.saveOrUpdate(dishDto);

        Long dishId = dishDto.getId();


        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item)->{

            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());


        //保存菜品口味数据到菜品口味表DISH_FLAVOR
        if(type == 0){
            dishFlavorService.saveBatch(flavors);
        }else if(type == 1){
            //删除原来FAVOR表中原数据，重新保存新数据
            LambdaQueryWrapper<DishFlavor>queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,dishId);
            dishFlavorService.remove(queryWrapper);
            dishFlavorService.saveBatch(flavors);

        }



    }

    @Transactional
    public DishDto useIdtoFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        //复制DISH到dto
        BeanUtils.copyProperties(dish,dishDto);
        //通过DISHID找到FAVOR
        LambdaQueryWrapper<DishFlavor>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        Long categoryId = dish.getCategoryId();
        Category byId = categoryService.getById(categoryId);
        dishDto.setCategoryName(byId.getName());


        return dishDto;



    }
    @Transactional
    public void deleteIdwithFlavor(Long id){
        Dish dish = this.getById(id);

        //通过DISHID找到FAVOR
        LambdaQueryWrapper<DishFlavor>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);

        //根据dishid删除FLAVOR信息
        dishFlavorService.remove(queryWrapper);

        //删除DISH表信息
        this.removeById(dish);




    }
}
